/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package generator;

import generator.dialog.ConnectionDialog;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import net.db.generator.BaseListModel;
import net.db.generator.JavaBaseClassWriter;
import net.db.generator.JavaConnectionPropertiesWriter;
import net.db.generator.JavaDAOBaseClassWriter;
import net.db.generator.JavaDAOClassWriter;
import net.db.generator.JavaDAOFullClassWriter;
import net.db.generator.field.models.BaseFieldModel;
import net.db.generator.field.models.DataBaseColumnType;
import net.db.generator.utils.ClassGeneratorUtil;
import net.etfbl.sqljdbc.SQLiteConnectionPool;

/**
 *
 * @author abosancic
 */
public class ClassGenerator extends javax.swing.JFrame {

    /**
     * Creates new form ClassGenerator
     */
    public ClassGenerator() {
        initComponents();
        generateBtn.setEnabled((dbGenList.getSelectedValue() != null));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        generateBtn = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        dbList = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        dbTablesList = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        dbColumnsList = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        dbGenList = new javax.swing.JList();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldDTO = new javax.swing.JTextField();
        jTextFieldDAO = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jPropertiesFile = new javax.swing.JCheckBox();
        jConnectionPoolClass = new javax.swing.JCheckBox();
        jFindRemoveMethods = new javax.swing.JCheckBox();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);
        getContentPane().setLayout(null);

        generateBtn.setFont(new java.awt.Font("Ubuntu", 0, 24)); // NOI18N
        generateBtn.setLabel("Generate");
        generateBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateBtnActionPerformed(evt);
            }
        });
        getContentPane().add(generateBtn);
        generateBtn.setBounds(470, 430, 335, 56);
        generateBtn.getAccessibleContext().setAccessibleName("generateBtn");

        dbList.setFont(new java.awt.Font("Ubuntu", 0, 16)); // NOI18N
        dbList.setForeground(new java.awt.Color(153, 153, 153));
        dbList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        dbList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                dbListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(dbList);
        dbList.getAccessibleContext().setAccessibleName("");

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(10, 32, 157, 252);

        dbTablesList.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        dbTablesList.setForeground(new java.awt.Color(102, 102, 102));
        dbTablesList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        dbTablesList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                dbTablesListMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(dbTablesList);

        getContentPane().add(jScrollPane2);
        jScrollPane2.setBounds(185, 32, 160, 252);

        dbColumnsList.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        dbColumnsList.setForeground(new java.awt.Color(102, 102, 102));
        dbColumnsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        dbColumnsList.setEnabled(false);
        jScrollPane3.setViewportView(dbColumnsList);

        getContentPane().add(jScrollPane3);
        jScrollPane3.setBounds(655, 32, 157, 252);

        jLabel1.setText("Schemas:");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(10, 11, 70, 14);

        jLabel2.setText("Tables:");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(185, 11, 60, 14);

        jLabel3.setText("Columns:");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(655, 11, 70, 14);

        jButton1.setFont(new java.awt.Font("Ubuntu", 0, 24)); // NOI18N
        jButton1.setLabel("Load schemas");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(10, 430, 335, 56);

        dbGenList.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        dbGenList.setForeground(new java.awt.Color(102, 102, 102));
        dbGenList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        dbGenList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                dbGenListMouseClicked(evt);
            }
        });
        dbGenList.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                dbGenListPropertyChange(evt);
            }
        });
        jScrollPane4.setViewportView(dbGenList);

        getContentPane().add(jScrollPane4);
        jScrollPane4.setBounds(470, 32, 167, 252);

        jButton2.setText("->");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(364, 89, 88, 23);

        jButton3.setText("All ->");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3);
        jButton3.setBounds(364, 129, 88, 23);

        jButton4.setText("<-");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton4);
        jButton4.setBounds(364, 170, 88, 23);

        jButton5.setText("<- All");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton5);
        jButton5.setBounds(363, 211, 89, 23);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("DTO package:");
        jLabel4.setToolTipText("");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(10, 300, 88, 17);

        jTextFieldDTO.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        jTextFieldDTO.setText("com.etfbl.net.dto");
        getContentPane().add(jTextFieldDTO);
        jTextFieldDTO.setBounds(10, 330, 330, 25);

        jTextFieldDAO.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        jTextFieldDAO.setText("com.etfbl.net.dao");
        getContentPane().add(jTextFieldDAO);
        jTextFieldDAO.setBounds(10, 390, 330, 25);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel5.setText("DAO package:");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(10, 370, 134, 17);

        jPropertiesFile.setSelected(true);
        jPropertiesFile.setText("Generate properties file.");
        getContentPane().add(jPropertiesFile);
        jPropertiesFile.setBounds(470, 330, 230, 23);

        jConnectionPoolClass.setSelected(true);
        jConnectionPoolClass.setText("Generate connectionpool class.");
        getContentPane().add(jConnectionPoolClass);
        jConnectionPoolClass.setBounds(470, 360, 250, 23);

        jFindRemoveMethods.setSelected(true);
        jFindRemoveMethods.setText("Generate find and remove methods.");
        getContentPane().add(jFindRemoveMethods);
        jFindRemoveMethods.setBounds(470, 300, 260, 23);

        jMenu1.setText("Main");

        jMenuItem2.setText("jMenuItem2");
        jMenu1.add(jMenuItem2);
        jMenu1.add(jSeparator1);

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem1.setText("Exit");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem3.setText("DB connection");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem3);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        setSize(new java.awt.Dimension(853, 566));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    protected void dbListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_dbListMouseClicked
        //System.out.println("schemas: " + dbList.getSelectedValue());
        if(dbList.getSelectedValue() != null){
            dbColumnsList.setModel(new BaseListModel(new String[]{}));
            dbGenList.setModel(new BaseListModel(new String[]{}));
            dbTablesList.setModel(new BaseListModel(getAllTables(dbList.getSelectedValue().toString())));
        }
    }//GEN-LAST:event_dbListMouseClicked

    protected void generateBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateBtnActionPerformed
        
        if (dbGenList.getModel() != null && dbGenList.getModel() instanceof BaseListModel) {
            String tables = null;
            
            JavaDAOClassWriter dao = new JavaSQLDAOClassWriter();
            dao.setPackageName(jTextFieldDAO.getText());
            dao.setImportPackages(new String[]{
                "net.db.generator.utils.ClassGeneratorUtil",
                "java.io.InputStream",
                "java.io.Serializable",
                "java.lang.reflect.InvocationTargetException",
                "java.lang.reflect.Method",
                "java.math.BigDecimal",
                "java.sql.Connection",
                "java.sql.Date",
                "java.sql.PreparedStatement",
                "java.sql.ResultSet",
                "java.sql.SQLException",
                "java.sql.Time",
                "java.sql.Timestamp",
                "java.util.ArrayList",
                "java.util.Arrays",
                "java.util.List",
                "java.util.logging.Level",
                "net.etfbl.dao.base.IDAOBase",
                "java.util.logging.Logger"
            });
            dao.generateClass();
            
            // write  ConnectionPool class
            if (jConnectionPoolClass.isSelected()) {
                JavaSQLConnectionClassWriter jcon = new JavaSQLConnectionClassWriter();
                jcon.setPackageName(jTextFieldDAO.getText());
                jcon.setImportPackages(new String[]{"java.sql.*", "java.util.*"});
                jcon.generateClass();
            }

            if (jPropertiesFile.isSelected()) {
                JavaConnectionPropertiesWriter jprop = new JavaConnectionPropertiesWriter();
                jprop.setPackageName(jTextFieldDAO.getText());
                jprop.setProperties(getConnectionProperties());
                jprop.generateFile();
            }
            
            // generate class
            JavaDAOBaseClassWriter daogen = new JavaDAOBaseClassWriter();
            if(jFindRemoveMethods.isSelected())
            {
                daogen = new JavaDAOFullClassWriter();
            }
            daogen.setPackageName(jTextFieldDAO.getText());
            
            JavaBaseClassWriter gen = new JavaBaseClassWriter();
            gen.setPackageName(jTextFieldDTO.getText());
            
            int size = dbGenList.getModel().getSize();
            for (int i = 0; i < size; i++) {
                
                List<DataBaseColumnType> columns = new ArrayList<>();
                tables = dbGenList.getModel().getElementAt(i).toString();
                
                gen.setClassName(tables);
                daogen.setClassName(tables);       
                daogen.setImportPackages(new String[]{jTextFieldDTO.getText() + "." + ClassGeneratorUtil.humanize(tables)});
                
                BaseListModel dbcolumns = new BaseListModel(getAllColumns(dbList.getSelectedValue().toString(), tables));
                for (int j = 0; j < dbcolumns.getSize(); j++) {
                    columns.add(DataBaseColumnType.toModel(dbcolumns.getElementAt(j).toString()));
                }
                
                String [] primaryKeyName = getPrimaryKeyName(tables);
                ArrayList<String> pkNames = new ArrayList<String>(Arrays.asList(primaryKeyName));
                gen.setUniqueField(primaryKeyName);
                
                List<BaseFieldModel> models = new ArrayList<BaseFieldModel>();
                for (DataBaseColumnType dataBaseColumnType : columns) {
                    BaseFieldModel model = new BaseFieldModel(dataBaseColumnType.getName(), SQLiteTypeMap.convert(dataBaseColumnType.getType()));
                    model.setPrimaryKey(pkNames.contains( dataBaseColumnType.getName() ));
                    models.add(model);
                }
                
                gen.setFields(models.toArray(new BaseFieldModel[models.size()]));
                
                
                gen.generateClass();
                
                daogen.setUniqueField(primaryKeyName);
                daogen.setFields(models.toArray(new BaseFieldModel[models.size()]));
                daogen.generateClass();
            }

            System.out.println("Generete complite !!!");
            
            JOptionPane.showMessageDialog(this, "Generating successfully completed.", "Class generator", JOptionPane.INFORMATION_MESSAGE);
            
        }
        
    }//GEN-LAST:event_generateBtnActionPerformed

    protected void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dbTablesList.removeAll();
        dbColumnsList.removeAll();
        dbList.setModel(new BaseListModel(new String[]{"sqlite_master"}));
    }//GEN-LAST:event_jButton1ActionPerformed

    protected void dbTablesListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_dbTablesListMouseClicked
        if(dbTablesList.getSelectedValue() != null){
            dbColumnsList.setModel(new BaseListModel(getAllColumns(dbList.getSelectedValue().toString(),
                    dbTablesList.getSelectedValue().toString())));
        }
    }//GEN-LAST:event_dbTablesListMouseClicked

    protected void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        synchronized (this) {
            if (dbTablesList.getSelectedValue() != null) {
                BaseListModel model = addFromListModel((BaseListModel) dbGenList.getModel(), dbTablesList.getSelectedValue().toString());
                dbGenList.setModel(model);
                model = removeFromListModel((BaseListModel) dbTablesList.getModel(), dbTablesList.getSelectedValue().toString());
                dbTablesList.setModel(model);
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    protected void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        synchronized (this) {
            if (dbTablesList.getModel() != null && dbTablesList.getModel() instanceof BaseListModel) {
                BaseListModel curentModel = (BaseListModel) dbTablesList.getModel();
                for (int i = 0; i < curentModel.getSize(); i++) {
                    BaseListModel model = addFromListModel((BaseListModel) dbGenList.getModel(), curentModel.getElementAt(i).toString());
                    dbGenList.setModel(model);
                    model = removeFromListModel((BaseListModel) dbTablesList.getModel(), curentModel.getElementAt(i).toString());
                    dbTablesList.setModel(model);
                }

            }
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    protected void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        synchronized (this) {
            if (dbGenList.getSelectedValue() != null) {
                BaseListModel model = addFromListModel((BaseListModel) dbTablesList.getModel(), dbGenList.getSelectedValue().toString());
                dbTablesList.setModel(model);
                model = removeFromListModel((BaseListModel) dbGenList.getModel(), dbGenList.getSelectedValue().toString());
                dbGenList.setModel(model);
            }
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    protected void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        synchronized (this) {
            if (dbGenList.getModel() != null && dbGenList.getModel() instanceof BaseListModel) {
                BaseListModel curentModel = (BaseListModel) dbGenList.getModel();
                for (int i = 0; i < curentModel.getSize(); i++) {
                    BaseListModel model = addFromListModel((BaseListModel) dbTablesList.getModel(), curentModel.getElementAt(i).toString());
                    dbTablesList.setModel(model);
                    model = removeFromListModel((BaseListModel) dbGenList.getModel(), curentModel.getElementAt(i).toString());
                    dbGenList.setModel(model);
                }

            }
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    protected void dbGenListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_dbGenListMouseClicked
        if(dbGenList.getSelectedValue() != null){
            dbColumnsList.setModel(new BaseListModel(getAllColumns(dbList.getSelectedValue().toString(),
                    dbGenList.getSelectedValue().toString())));
        }
    }//GEN-LAST:event_dbGenListMouseClicked

    protected void dbGenListPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_dbGenListPropertyChange
        generateBtn.setEnabled((dbGenList.getModel().getSize() > 0));
    }//GEN-LAST:event_dbGenListPropertyChange

    protected void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    @SuppressWarnings("empty-statement")
    protected void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        new ConnectionDialog(this, rootPaneCheckingEnabled).setVisible(rootPaneCheckingEnabled);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    protected BaseListModel addFromListModel(BaseListModel model, String item){
        
        List<String> columns = new ArrayList<>();
        for (int i = 0; i < model.getSize(); i++) {
            columns.add(model.getElementAt(i).toString()); 
        }
//        if(!columns.contains(item));
            columns.add(item);
        return new BaseListModel(columns.toArray(new String[columns.size()]));
        
    }
    
    protected BaseListModel removeFromListModel(BaseListModel model, String item){
        
        List<String> columns = new ArrayList<>();
        for (int i = 0; i < model.getSize(); i++) {
            if(!model.getElementAt(i).toString().equals(item))
                columns.add(model.getElementAt(i).toString()); 
        }
        return new BaseListModel(columns.toArray(new String[columns.size()]));
        
    }
    
    protected String[] getAllTables(String schema) {
       ArrayList<String> allTables = new ArrayList<>();
        
        try {
            SQLiteConnectionPool cp = SQLiteConnectionPool.getConnectionPool();
            Connection conn = cp.checkOut();     
            try {

                ResultSet res = conn.createStatement().executeQuery("SELECT * FROM " +
                                             " " + schema + " " +
                                             "WHERE type='table'" +
                                             "ORDER BY name");
            while (res.next()) {
                   String tableName = res.getString(2);
                   allTables.add(tableName);
            }

            } catch (SQLException ex) {
                Logger.getLogger(ClassGenerator.class.getName()).log(Level.SEVERE, null, ex);
            }
            cp.checkIn(conn);

        } catch (Exception ex) {
            Logger.getLogger(ClassGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return allTables.toArray(new String[allTables.size()]);
    }
    
    protected String[] getAllColumns(String schema, String tabel) {
        ArrayList<DataBaseColumnType> allColumns = new ArrayList<>();
        try {
            SQLiteConnectionPool cp = SQLiteConnectionPool.getConnectionPool();
            Connection conn = cp.checkOut();
            try {
                
            Statement st = conn.createStatement();    
            ResultSet res = st.executeQuery("SELECT * FROM " +
                                             " sqlite_master " +
                                             "WHERE type='table' " + "AND name = '" + tabel + "'"
                                             );
            
            String dll = "";
            while (res.next()) {
                    dll = res.getString(5);
                    break;
            }
                
                 SQLiteParser parser = new SQLiteParser(dll);
                    System.out.println(parser);
           Map columns = parser.getColumns();
           Set entry  = columns.entrySet();
                for (Iterator it = entry.iterator(); it.hasNext();) {
                    Object object = it.next();
                     System.out.println("getPrimaryKeys(): columnName=" );
                     String [] typeName = object.toString().split("=");

                    String columnName = typeName[0];
                    String dataType = typeName[1];
                    int size = 0;

                    DataBaseColumnType ct = new DataBaseColumnType(columnName, dataType, size);

                    allColumns.add(ct);
                }
                res.close();
                st.close();

            } catch (SQLException ex) {
                Logger.getLogger(ClassGenerator.class.getName()).log(Level.SEVERE, null, ex);
            }
            cp.checkIn(conn);

        } catch (Exception ex) {
            Logger.getLogger(ClassGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        ArrayList<String> tmp = new ArrayList<>();
        for (DataBaseColumnType type : allColumns) {
            tmp.add(type.toString());
        }
        
        return tmp.toArray(new String[tmp.size()]);
    }
    
    protected String [] getPrimaryKeyName(String table){
        ArrayList<String> pkname = new ArrayList<String>();
        try {
            
            SQLiteConnectionPool cp = SQLiteConnectionPool.getConnectionPool();
            Connection conn = cp.checkOut();    
            DatabaseMetaData meta = conn.getMetaData();
            ResultSet rs = meta.getPrimaryKeys(dbList.getSelectedValue().toString(), null, table);
 
            while (rs.next()) {
                String columnName = rs.getString("COLUMN_NAME");
                //System.out.println("getPrimaryKeys(): columnName=" + columnName);
                pkname.add(columnName);
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(ClassGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
        return pkname.toArray(new String[pkname.size()]);
    }
    
    private java.util.Properties getConnectionProperties() {
        java.util.Properties p = new java.util.Properties();
        try {
            p.put("username", "");
            p.put("password", "");
            p.put("driver", "org.sqlite.JDBC");
            p.put("jdbcURL", "C:\\Users\\abosancic\\Repozitorij\\google\\trunk\\java\\SQLiteClassGenerator\\db\\db.db" + "?autoReconnect=true");
            p.put("preconnectCount", "5");
            p.put("maxIdleConnections", "25");
            p.put("maxConnections", "25");

        } catch (Exception ex) {
            Logger.getLogger(ClassGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }

        return p;
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ClassGenerator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ClassGenerator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ClassGenerator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ClassGenerator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ClassGenerator().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList dbColumnsList;
    private javax.swing.JList dbGenList;
    private javax.swing.JList dbList;
    private javax.swing.JList dbTablesList;
    private javax.swing.JButton generateBtn;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    protected javax.swing.JCheckBox jConnectionPoolClass;
    protected javax.swing.JCheckBox jFindRemoveMethods;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    protected javax.swing.JCheckBox jPropertiesFile;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JTextField jTextFieldDAO;
    private javax.swing.JTextField jTextFieldDTO;
    // End of variables declaration//GEN-END:variables
}
